﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DCT.ViewModel;

namespace DCT.View
{
    public class MiscellaneousDeviceView : DeviceBaseView
    {
        #region Constructor

        public MiscellaneousDeviceView()
            : base()
        {
        }
                     
        #endregion // Constructor

        #region Fields

        MiscellaneousDeviceViewModel _mdViewModel;

        #endregion // Fields

        #region Methods

        public override void UpdateVisual()
        {
            base.UpdateVisual();

            _deviceVisual.Children.Clear();

            DrawingVisual drawingVisual;
            DrawingContext drawingContext;

            if (!string.IsNullOrEmpty(_mdViewModel.MDBitmap))
            {
                drawingVisual = new DrawingVisual();
                drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawImage(new BitmapImage(new Uri("DeviceGraph\\" + _mdViewModel.MDBitmap, UriKind.Relative)), new Rect(0, 0, _mdViewModel.DeviceWidth, _mdViewModel.DeviceHeight));
                drawingContext.Close();
                _deviceVisual.Children.Add(drawingVisual);
            }

            if (!string.IsNullOrEmpty(_mdViewModel.MDString))
            {
                drawingVisual = new DrawingVisual();
                drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawText(
                    new FormattedText(_mdViewModel.MDString,
                        CultureInfo.GetCultureInfo("en-us"),
                        FlowDirection.LeftToRight,
                        new Typeface("Arial"),
                        12,
                        Brushes.LightSeaGreen),
                        new Point(0, 0));
                drawingContext.Close();
                _deviceVisual.Children.Add(drawingVisual);
            }
            
        }

        protected override void DeviceView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            base.DeviceView_DataContextChanged(sender, e);

            if (e.NewValue is MiscellaneousDeviceViewModel)
            {
                _mdViewModel = e.NewValue as MiscellaneousDeviceViewModel;
                _mdViewModel.UpdateVisual += UpdateVisual;
            }
        }

        #endregion // Methods
    }
}
